# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import psycopg2
from amazon.postgres import db
from amazon.items import *
from scrapy.conf import settings

class AmazonPipeline(object):
    def __init__(self):
        # 数据库连接信息
        data_conn_string = ' '.join(["host=", settings['DATA_DB']['host'], "dbname=", settings['DATA_DB']['dbname'], \
                                     "user=", settings['DATA_DB']['user'], "password=", settings['DATA_DB']['password'],
                                     "port=", settings['DATA_DB']['port']])
        self.data_conn = psycopg2.connect(data_conn_string)
        self.data_session = db(self.data_conn)

    def process_item(self, item, spider):
        if isinstance(item, AmazonItem):
            table_name = '"dc16"."amazon_category"'
            try:
                with self.data_conn:
                    self.data_session.Insert(table_name, item)
            except Exception as e:
                print e
        return item
